using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace AgenciaEmpleo.Plantillas
{
    public partial class frmMoldeWizard : Form
    {

        #region Singleton

        static private frmMoldeWizard frm = null;

        public frmMoldeWizard()
        {
            InitializeComponent();
        }

        static public frmMoldeWizard GetInstance()
        {
            if (frm == null || frm.IsDisposed == true)
            {
                frm = new frmMoldeWizard();
            }
            frm.BringToFront();
            return frm;
        }

        #endregion

        private IList _pages = new ArrayList();
        private WizardPage _activePage;

        private void frmMoldeWizard_Load(object sender, EventArgs e)
        {
            if (_pages.Count != 0)
            {
                ResizeToFit();
                SetActivePage(0);
            }
            else
            {
                SetWizardButtons(WizardButtons.None);
            }
        }

        public IList Pages
        {
            get { return _pages; }
        }

        private WizardPage GetActivePage()
        {
            return _activePage;
        }

        private void ResizeToFit()
        {
            Size maxPageSize = new Size(pnlInferior.Width, 0);

            foreach (WizardPage page in _pages)
            {
                if (page.Width > maxPageSize.Width)
                    maxPageSize.Width = page.Width;
                if (page.Height > maxPageSize.Height)
                    maxPageSize.Height = page.Height;
            }

            foreach (WizardPage iPage in _pages)
            {
                iPage.Size = maxPageSize;
            }

            Size extraSize = this.Size;
            extraSize -= pnlCentral.Size;

            Size newSize = maxPageSize + extraSize;
            this.Size = newSize;
        }

        private int GetActiveIndex()
        {
            WizardPage mActivePage = GetActivePage();

            for (int i = 0; i < _pages.Count; ++i)
            {
                if (mActivePage == _pages[i])
                    return i;
            }

            return -1;
        }

        public void SetActivePage(int pageIndex)
        {
            if (pageIndex < 0 || pageIndex >= _pages.Count)
                throw new ArgumentOutOfRangeException("pageIndex");

            WizardPage mPagina = (WizardPage)_pages[pageIndex];
            SetActivePage(mPagina);
        }

        private WizardPage FindPage(string pageName)
        {
            foreach (WizardPage page in _pages)
            {
                if (page.Name == pageName)
                    return page;
            }

            return null;
        }

        private void SetActivePage(string newPageName)
        {
            WizardPage newPage = FindPage(newPageName);

            if (newPage == null)
                throw new Exception(string.Format("Can't find page named {0}", newPageName));

            SetActivePage(newPage);
        }

        private void SetActivePage(WizardPage newPage)
        {
            WizardPage oldActivePage = _activePage;

            // Si la pagina no esta en Controls collection, agregarla.
            if (!pnlCentral.Controls.Contains(newPage))
                pnlCentral.Controls.Add(newPage);

            // Mostrar la pagina.
            newPage.Visible = true;

            _activePage = newPage;
            CancelEventArgs e = new CancelEventArgs();
            newPage.OnSetActive(e);

            if (e.Cancel)
            {
                newPage.Visible = false;
                _activePage = oldActivePage;
            }

            // Ocultar todas las otras paginas.
            foreach (WizardPage page in _pages)
            {
                if (page != _activePage)
                    page.Visible = false;
            }
        }

        internal void SetWizardButtons(WizardButtons buttons)
        {
            // Habilitar el boton anterior
            btnAtras.Enabled = ((buttons & WizardButtons.Back) != 0);

            // Habilitar el boton Finalizar y esconder el Siguiente
            if ((buttons & WizardButtons.Finish) != 0)
            {
                btnFinalizar.Visible = true;
                btnFinalizar.Enabled = true;

                btnSiguiente.Visible = false;
                btnSiguiente.Enabled = false;

                this.AcceptButton = btnFinalizar;
            }
            else // Habilitar el boton Siguiente y esconder el Finalizar
            {
                btnFinalizar.Visible = false;
                btnFinalizar.Enabled = false;

                btnSiguiente.Visible = true;
                btnSiguiente.Enabled = ((buttons & WizardButtons.Next) != 0);

                this.AcceptButton = btnSiguiente;
            }
        }

        internal void EnableCancelButton(bool enableCancelButton)
        {
            btnCancelar.Enabled = enableCancelButton;
        }

        private void btnAtras_Click(object sender, EventArgs e)
        {
            WizardPageEventArgs wpea = PreChangePage(-1);
            _activePage.OnWizardBack(wpea);
            PostChangePage(wpea);
        }

        private void btnSiguiente_Click(object sender, EventArgs e)
        {
            WizardPageEventArgs wpea = PreChangePage(+1);
            _activePage.OnWizardNext(wpea);
            PostChangePage(wpea);
        }

        private void btnFinalizar_Click(object sender, EventArgs e)
        {
            CancelEventArgs cea = new CancelEventArgs();
            _activePage.OnWizardFinish(cea);
            if (cea.Cancel)
                return;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private WizardPageEventArgs PreChangePage(int delta)
        {
            // Figure out which page is next.
            int activeIndex = GetActiveIndex();
            int nextIndex = activeIndex + delta;

            if (nextIndex < 0 || nextIndex >= _pages.Count)
                nextIndex = activeIndex;

            // Fill in the event args.
            WizardPage newPage = (WizardPage)_pages[nextIndex];

            WizardPageEventArgs e = new WizardPageEventArgs();
            e.NewPage = newPage.Name;
            e.Cancel = false;

            return e;
        }

        private void PostChangePage(WizardPageEventArgs e)
        {
            if (!e.Cancel)
                SetActivePage(e.NewPage);
        }

   }

    [Flags]
    public enum WizardButtons
    {
        None = 0x0000,
        Back = 0x0001,
        Next = 0x0002,
        Finish = 0x0004,
    }
}